﻿//------------------------------------------------------------------------------------------------- 
// <copyright file="ConnectWebParts.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>Creates a new SPWebApplication.</summary>
//-------------------------------------------------------------------------------------------------
namespace Collaboris.Saf.Actions.Wss.WebPart
{
    using Engine;

    using Entities;

    using Microsoft.SharePoint;

    using SharePoint.Wss.Attributes;
    using SharePoint.Wss.Files;
    using SharePoint.Wss.WebParts;

    using Utils;

    [CollaborisClass(
        Title = "Adds WebParts to a specified Web Part Zone",
        ReleaseStatus = ReleaseStatus.Development,
        DisableDocumentation = false,
        ContactAuthorEmail = "Support@Collaboris.co.uk",
        Summary = "Allows multiple web parts to be added to a web part zone " + 
                  "on a web part page.",
        ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C663}"
        )
    ]
    public class AddWebParts : WSSActionBase<WebPartZoneInfo>
    {
        private string folderUrl;

        private string pageName;

        /// <summary>
        /// Does the action.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        protected override ProcessStatus DoAction(SafContext context, WebPartZoneInfo entity)
        {
            this.Trace.TraceMethodStart("DoAction");
            
            // now lets go connect em up.
            SPFile targetFile = SPFileHelper.GetFile(this.TargetWeb, this.FolderUrl, this.PageName);

            ValidationHelper.VerifyObjectArgument(targetFile, "targetFile");

            SPWebPartHelper.AddWebParts(
                                TargetWeb,
                                targetFile,
                                entity.WebParts,
                                entity.ZoneId,
                                entity.ClearZone.Value,
                                entity.PersonalizationScope);

            this.Trace.TraceMethodEnd("DoAction");
            
            return ProcessStatus.Success;
        }

        [CollaborisProperty(
           Summary = "Specifies the name of the page located in 'Folder Url'.",
           Title = "Page Name",
           Example = "default.aspx",
           InputHint = InputHint.SingleText,
           Required = true)]
        public string PageName
        {
            get
            {
                return this.pageName;
            }
            set
            {
                this.pageName = value;
            }
        }

        [CollaborisProperty(
           Summary = "Specifies the name of the list in the current web.",
           Title = "Folder Url",
           Example = @"${CurrentWeb.Url}/Pages",
           InputHint = InputHint.Url,
           Required = true)]
        public string FolderUrl
        {
            get
            {
                return this.folderUrl;
            }
            set
            {
                this.folderUrl = value;
            }
        }
    }
}